package payment;

import java.util.Date;
import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

import payment.api.Pay;

public class Pays {
    private static Timer timer = new Timer();
    private static HashMap<String, Status> pays = new HashMap<>();

    public static enum Status {
        ALIPAY,
        NONE,
        CANCEL,
        TIMEOUT,
        WECHAT
    }

    public static boolean newPay(String token, Date timeoutDate) {
        System.out.println("[Pays] [new] token: " + token + ", timeoutDate: " + timeoutDate.toString());
        if (pays.containsKey(token)) {
            System.out.println("[Pays] [new] Existed, token: " + token);
            return false;
        }
        pays.put(token, Status.NONE);
        System.out.println("[Pays] [new] Added, token: " + token);
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (pays.get(token).equals(Status.NONE)) {
                    System.out.println("[Pays] [timeoutTimer] Timeout, token: " + token);
                    pays.put(token, Status.TIMEOUT);
                }
            }
        }, timeoutDate);
        return true;
    }

    /**
     * 确认支付
     * 
     * @param token 订单号
     * @return {@code Pays.Status}，无此订单为{@code null}
     */
    public static Status pay(String token, Pay.PayMethod method) {
        System.out.println("[Pays] [pay] token: " + token);
        if (pays.get(token) == null) {
            System.out.println("[Pays] [pay] Not found, token:" + token);
            return null;
        } else if (pays.get(token).equals(Status.NONE)) {
            if (method.equals(Pay.PayMethod.WX))
            pays.put(token, Status.WECHAT);
            if (method.equals(Pay.PayMethod.ZFB))
            pays.put(token, Status.ALIPAY);
            System.out.println("[Pays] [pay] Pay success, token: " + token + ", method: " + pays.get(token).toString());
        }
        return pays.get(token);
    }

    /**
     * 获取支付状态
     * 
     * @param token 订单token
     * @return {@code Pays.Status}，无此订单为{@code null}
     */
    public static Status getPayStatus(String token) {
        System.out.println("[Pays] [status] token: " + token + ", status: " + pays.get(token).toString());
        return pays.get(token);
    }

    public static boolean cancelPay(String token) {
        if (pays.containsKey(token)) {
            if (pays.get(token).equals(Status.NONE)) {
                pays.put(token, Status.CANCEL);
                System.out.println("[Pays] [cancel] Success, token: " + token);
                return true;
            }
            if (pays.get(token).equals(Status.ALIPAY) || pays.get(token).equals(Status.WECHAT)) {
                System.out.println("[Pays] [cancel] Paid, token: " + token);
                return false;
            } else {
                System.out.println("[Pays] [cancel] token: " + token + ", status: " + pays.get(token).toString());
                return true;
            }
        }
        System.out.println("[Pays] [cancel] Not found, token: " + token);
        return false;
    }
}
